package customControls;

import android.content.Context;
import android.content.Intent;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import com.example.jaden.washer.*;

import org.json.JSONException;
import org.json.JSONObject;

import java.net.MalformedURLException;
import java.util.List;

import activity.OrderPage;
import order.Order;

public class OrderToBeConfirmedAdapter extends ArrayAdapter<Order> implements View.OnClickListener {

    Order order;
    int Position;
    private int resourceId;
    private static final String TAG = "OrderToBeConfirmedAdapt";
    public OrderToBeConfirmedAdapter(@NonNull Context context, int resource, List<Order> objects) {
        super(context,resource,objects);
        resourceId = resource;
    }
    public class ViewHolder
    {
        TextView orderID;
        TextView washerID;
        TextView price;
        TextView number;
        TextView orderstatus;
        Button deleteinfo;
        Button confirm;
    }
    @NonNull
    @Override
    public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {

        //根据position 得到当前子项的Commodity实例
        order=getItem(position);
        Position=position;
       ViewHolder viewHolder = null;
        if(convertView==null)
        {
            Log.d(TAG, "getView: "+position);
            //为当前子项加载指定布局->resourceId
            convertView=LayoutInflater.from(getContext()).inflate(resourceId,parent,false);
            viewHolder=new ViewHolder();
            //获取控件实例
            getInstance(viewHolder,convertView);
            convertView.setTag(viewHolder);
        }
        else {
            viewHolder= (ViewHolder) convertView.getTag();
        }
        setClickListener(viewHolder);
        //显示控件信息
        showInformation(viewHolder);
        return convertView;
    }


    //获取控件实例
    private void getInstance(ViewHolder viewHolder, View view) {
        viewHolder.orderID=(TextView)view.findViewById(R.id.textView_orderID_orderlistviewitemtobecomfirmed);
        viewHolder.washerID=(TextView)view.findViewById(R.id.textView_washerID_orderlistviewitemtobecomfirmed);
        viewHolder. price=(TextView)view.findViewById(R.id.textView_price_orderlistviewitemtobecomfirmed);
        viewHolder.number=(TextView)view.findViewById(R.id.textView_number_orderlistviewitemtobecomfirmed);
        viewHolder.orderstatus=(TextView)view.findViewById(R.id.textView_orderstatus_orderlistviewitemtobecomfirmed);
        viewHolder.deleteinfo=(Button)view.findViewById(R.id.button_deleteorder_orderlistviewitemtobecomfirmed);
        viewHolder.confirm=(Button)view.findViewById(R.id.button_validate_orderlistviewitemtobecomfirmed);
    }
    //显示控件信息
    private void showInformation(ViewHolder viewHolder) {

        viewHolder.orderID.setText(viewHolder.orderID.getText()+String.valueOf(order.getOrderID()));
        viewHolder.washerID.setText(viewHolder.washerID.getText()+String.valueOf(order.getWasherID()));
        viewHolder.price.setText(viewHolder.price.getText()+String.valueOf(order.getOrderMoney())+"￥");
        viewHolder.number.setText(viewHolder.number.getText()+String.valueOf(++Position));
        viewHolder.orderstatus.setText("预约中");
    }
   private void setClickListener(ViewHolder viewHolder)
   {
       viewHolder.confirm.setOnClickListener(this);
       viewHolder.deleteinfo.setOnClickListener(this);
   }


    @Override
    public void onClick(View v) {
        MyApplication application;
        String url;
        JSONObject userJson;
        String messageJson;
        switch (v.getId()){
            case R.id.button_deleteorder_orderlistviewitemtobecomfirmed:
                /**
                 * 取消订单
                 */
                //打包Json数据

                try {
                    url="http://47.96.159.236:8080/Wash/Order?method=StudentWithdraw";
                    userJson=new JSONObject();
                    userJson.put("StuID",order.getStuID());
                    userJson.put("OrderID",order.getOrderID());
                    messageJson=String.valueOf(userJson);
                    TransferData.DeleteOrder(url,messageJson);
                } catch (JSONException e) {
                    e.printStackTrace();
                } catch (MalformedURLException e) {
                    e.printStackTrace();
                }

                application=MyApplication.getInstance();
                while(true) {

                    if(application.isDeleteOK()) {
                       application.setDeleteOK(false);
                        OrderPage orderPage=OrderPage.getApp();
                        orderPage.getOrder(1);
                        break;
                    }
                }

                break;
            case R.id.button_validate_orderlistviewitemtobecomfirmed:

                try {
                    url="http://47.96.159.236:8080/Wash/Order?method=StudentConfirm";
                    userJson=new JSONObject();
                    userJson.put("StuID",order.getStuID());
                    userJson.put("OrderID",order.getOrderID());
                    messageJson=String.valueOf(userJson);
                    TransferData.ConfirmOrder(url,messageJson);
                } catch (JSONException e) {
                    e.printStackTrace();
                } catch (MalformedURLException e) {
                    e.printStackTrace();
                }

                application=MyApplication.getInstance();
                while(true) {
                    if(application.isConfirmOK()) {
                        application.setConfirmOK(false);
                        Toast.makeText(getContext(), application.getResponseMessage().getMsg(), Toast.LENGTH_LONG).show();
                        OrderPage orderPage=OrderPage.getApp();
                        orderPage.getOrder(1);
                        break;
                    }
                }

                break;
        }
    }

    public void verification(ResponseMessage responseMessage) {
        Intent intent;
        switch (responseMessage.getCode()) {
            case 5040:
                Toast.makeText(getContext(), responseMessage.getMsg(), Toast.LENGTH_LONG).show();

                break;
            case 5041:
                Toast.makeText(getContext(), responseMessage.getMsg(), Toast.LENGTH_LONG).show();
                break;
            case 5050:
                Toast.makeText(getContext(), responseMessage.getMsg(), Toast.LENGTH_LONG).show();
                intent=new Intent(getContext(), OrderPage.class);
                getContext().startActivity(intent);
                break;
            case 5051:
                Toast.makeText(getContext(), responseMessage.getMsg(), Toast.LENGTH_LONG).show();
                break;
                default:
                    break;
        }
    }
}
